<html>
<head><title>Calculator</title></head>
<body>

<h1>Calculator</h1>
Type an expression in the following box (e.g., (3+3/7)*7).
<form method="GET">
<input type="text" name="expr"><input type="submit" value="Calculate">
</form>
</body>
</html>

<?php
$expr = $_GET['expr'];
if( $expr != '' ) 
{
    echo '<h2>Result:</h2>';
    // eval('$result = '.$expr.';');
    //echo $expr, '<br/>';
    $syntax_error = false;
    if( preg_match('/[^0-9\+\-\*\/\(\)\.]/', $expr) )
        $syntax_error = true;
    $return = @eval('$result = '.$expr.';');
    if( $return === false && ( $error = error_get_last( ) ) )
    {
        $syntax_error = true;
        //echo $error['type'], '<br/>';
        //echo $error['message'], '<br/>';
        //echo $error['file'], '<br/>';
        //echo $error['line'], '<br/>';
    }
    if( $syntax_error === true )
    {
        echo "Invalid input expression $expr";
    }
    else
        echo "$expr = $result";
}
?>

</body>
</html>
